/**
 * <p>Title: MonitorSMS.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-7-11 上午07:59:53  高强  Create
 *</pre>
 */
package com.baosight.sczx.common.zm.basetools.listener;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.cInterface.CmesConstants;
import com.baosight.sczx.zm.ct.domain.ZMCTSmsBoxsending;

/**
 * 定时监控短信发送
 * <p>Title: RegularSMS.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强  
 * @version 1.0
 * <pre>Histroy:
 *         2012-7-11 上午07:59:53  高强  Create
 *</pre>
 */
public class MonitorSMS {
	private static int count = 0;
	private String beginTime = StaticTools.getDateTime();
	private String endTime = StaticTools.getDateTime(10, beginTime, "yyyy-MM-dd HH:mm:ss");
	private Dao dao;

	
	public MonitorSMS(Dao dao){
		this.dao = dao;
	}
	
	/**
	 * 监控短信发送条数，超过预警值则停止短信服务
	 * <p>Function: MakeSMS</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2012-7-11 上午07:59:53  高强  
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void monitor()throws Exception{
		Map _map = new HashMap();
		if(count ==0)
		{
			List<Map> list=dao.query("ZMCTSmsBoxsended.query", _map);
			count = list.size();
		}else
		{
			List<Map> list=dao.query("ZMCTSmsBoxsended.query", _map);
			if(list.size()- count >400)
			{
				dao.update("ZMCTSystem.updateStart",_map);
				CmesConstants.SYSTEMSETTING_SMS = 1;//更新缓存
			}
			count = list.size();
		}	
	}
	
	/**
	 * 如果待发送表里在监控周期中有没有发送的短信 则 重启服务
	 * <p>Function: Check</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2012-7-11 上午07:59:53  高强  
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	public void Check()throws Exception{
	List list=dao.query("ZMCTSmsBoxsending.query", null);	
		for(int i=0; i<list.size(); i++)			
		{
			ZMCTSmsBoxsending SmsBoxsending =(ZMCTSmsBoxsending)list.get(i);
			String insertTime =  SmsBoxsending.getInserttime();
			//判断模板设置的生成时间是否在本次定时活动的范围内
			if(beginTime.compareTo(insertTime.toString()) <= 0 && endTime.compareTo(insertTime.toString()) > 0)
			{
			   continue;
			}else
			{
				  Runtime rn = Runtime.getRuntime();	
				  String s; 
				  int count=0;
				  Process processStart = rn.exec("cmd /k c:\\SMS\\startMonitor.bat");
				  InputStream in=processStart.getInputStream();  
				  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));  
				  while((s=bufferedReader.readLine()) != null){  
					  if(s.contains("startMain.bat"))
					  {
						  count++;
					  }
					  if(count>2)
					  {
						  Runtime rn1 = Runtime.getRuntime();	
						  rn1.exec("cmd /c  c:\\SMS\\stopMonitor.bat");
						  in.close();  
						  bufferedReader.close();
						  break;
					  }
				  }  
				  in.close();  
				  bufferedReader.close();  
				  break;
			}
		}
	}
	
	/**
	 * 如果待发送表里在的短信超过一天没有发送则删除
	 * <p>Function: Delete</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2012-7-11 上午07:59:53  高强  
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void Delete()throws Exception{
		
		List list=dao.query("ZMCTSmsBoxsending.query", null);	
		for(int i=0; i<list.size(); i++)			
		{
			ZMCTSmsBoxsending SmsBoxsending =(ZMCTSmsBoxsending)list.get(i);
			String insertTime = SmsBoxsending.getInserttime();
			String keyId = SmsBoxsending.getId().toString();
			String deleteTime = StaticTools.getDateTime(60*24, insertTime, "yyyy-MM-dd HH:mm:ss");
			if( beginTime.compareTo(deleteTime.toString()) > 0)
			{
				Map inMap = new HashMap();
				inMap.put("id", keyId);
				dao.delete("ZMCTSmsBoxsending.delete", inMap);
			}
		}
	}
}
